<?php

namespace app\common\model\xiluedu;

use app\common\library\Auth;
use think\Db;
use think\Log;
use think\Model;
use traits\model\SoftDelete;
use function fast\array_get;

class OfflineCourse extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xiluedu_offline_course';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state',
        'state_text',
        'start_time_text',
        'end_time_text',
        'enroll_end_time_text',
        'status_text'
    ];

    public function getEnrollEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['enroll_end_time']) ? $data['enroll_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }

    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getStateAttr($value,$data){
        if($data['start_time'] > time()){
            return 1; //即将开始
        }else if($data['start_time'] < time()){
            return 2; //已经开始
        }else{
            return 0; //已经过期
        }
    }

    public function getStateTextAttr($value,$data){
        $state_attr = [1 => '未开始', 2 => '进行中', 3 => '已结束'];
        $state = $this->getAttr('state');
        return $state_attr[$state];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getThumbImageAttr($value,$data)
    {
        return $value ? cdnurl($value,true) : '';
    }


    public function teacher(){
        return $this->belongsTo('Teacher','teacher_id','id',[],'LEFT')->setEagerlyType(1);
    }


    /**
     * 线下课程列表
     * @param $params
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function searchLists($params){
        $pagesize = array_get($params,'pagesize',10);

        $query = static::with(['teacher'=>function($query){
            $query->withField('id,name,job');
        }]);
        if($q = array_get($params,'q')){
            $query->where('name','like',"%$q%");
        }
        $paginator = $query->field('weigh,content,createtime,updatetime,deletetime',true)
            ->where('status','1')
            ->order('weigh','desc')
            ->paginate($pagesize)->each(function ($row){
                $left_count = $row->total_count - $row->enroll_count;
                $row->left_count = $left_count < 0 ? $left_count :$left_count;
            });
        return $paginator;
    }
}
